Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

115
Vistas
Passing argument to async / await function returns "undefined"

When posting data to an API and get a response, if I hardcode the body data inside the fetch call (body: "XYZ12345") it works fine, this is an example:

            const vatValidationRequest =
                fetch(
                    '/api/vies/validateVAT.php', {
                        method: 'POST',
                        body: "XYZ12345",
                        headers: {
                            'Content-Type': 'application/text'
                        }
                    })
                .then((response) => response.text())
                .then((responseText) => {
                    return responseText;
                });


            const validateVAT = async () => {
                const viesResponse = await vatValidationRequest;
                console.log(viesResponse);
            };

            validateVAT();

However, if I try to pass the body data as an argument (body: vatNumber), the validateVAT() function returns "undefined". This is what's not working:

            const vatValidationRequest = (vatNumber) => {
                fetch(
                    '/api/vies/validateVAT.php', {
                        method: 'POST',
                        body: vatNumber,
                        headers: {
                            'Content-Type': 'application/text'
                        }
                    })
                .then((response) => response.text())
                .then((responseText) => {
                    return responseText;
                });
            }


            const validateVAT = async (vatNumber) => {
                const viesResponse = await vatValidationRequest(vatNumber);
                console.log(viesResponse);
            };

            validateVAT("XYZ12345");

Any clues about how to pass the argument to the async function? thanks!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The problem is that you are not returning the response from the method. You should do this:

            const vatValidationRequest = (vatNumber) => {
                return fetch(
                    '/api/vies/validateVAT.php', {
                        method: 'POST',
                        body: vatNumber,
                        headers: {
                            'Content-Type': 'application/text'
                        }
                    })
                .then((response) => response.text())
                .then((responseText) => {
                    return responseText;
                });
            }


            const validateVAT = async (vatNumber) => {
                const viesResponse = await vatValidationRequest(vatNumber);
                console.log(viesResponse);
            };

            validateVAT("XYZ12345");
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda